<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/jquery-3.1.1.min.js"></script>
		<script type="text/javascript" src="../js/layui/layui.js"></script>
		<link rel="stylesheet" type="text/css" href="../js/layui/css/layui.css" />
	</head>
	<body>
 <div class="demo-tree demo-tree-box" id="test9" style="width: 100%; height: 545px; overflow: scroll;"></div>
	</body>
	<script>
	var index;
	var indextree;
	var token = window.localStorage.getItem("token");
		if(token==""||token==null){
			window.location="../login.html";
		}
$(function(){
	layui.use(['tree', 'util'], function(){
		  var tree = layui.tree
		  ,layer = layui.layer
		  ,util = layui.util
		  //开启节点操作图标
		$.ajax({
					url:"../GetAllModuleLayUI",
					data:{
						token:token
					},
					dataType:"json",
					type:"post",
			success:function(res){
				if(res.success==false){
					if(res.message==0){
						alert(res.remark);
						window.location="login.html";
					}else{
						alert(res.remark);
					}
				}else if(res!=null){
						indextree = tree.render({
						    elem: '#test9'
						    ,data: res
						    ,id: 'demoId1'
						    //,isJump: true //是否允许点击节点时弹出新窗口跳转
						    //,showCheckbox: true  //是否显示复选框
						    //,customOperate:true //节点操作自定义
						    ,edit: ['add', 'update', 'del'] //操作节点的图标
						    ,click: function(obj){
						    	//alert(obj.data.href);
						    }
						    
						    ,operate: function(obj){
						    //operate	click
						    		var type = obj.type;				//得到操作类型
						    		var data = obj.data;				//得到当前节点数据
						    		var elem = obj.elem;				//得到当前节点元素
						    		var deptId = data.id;				//得到节点索引
						    		var href = data.href;				//得到节点路径
						    		var title = data.title;				//得到节点标题
						    		var parentId = data.parentId;		//父节点
						    		if(type === 'add'){					//添加
											// alert("添加");
												layui.use("layer",function(){
													var layer = layui.layer;
													document.getElementById("adds").reset();
													$("#addparentId").val(deptId);
													index = layer.open({
														type:1
														,title:"添加"
														,content:$("#add")
														,area:["",""]
													})
												}) 
						    		//alert("添加");
						    		}else if(type === 'update'){		//修改
						    			//alert(data.id);
						    		$.ajax({
												url:"../GetModuleById",
												data:{
													id:data.id,
													token:token
												},
												dataType:"json",
												type:"post",
												success:function(reas){
													if(reas.message==0){
														layer.msg(res.remark,function(){
															window.location="../login.html";
														});
													}else{
														//var reaa = reas.message;
														var jsons = reas.message;
														var mId = jsons.id;
														var name = jsons.name
														var parentIds = jsons.parentId;
														var path = jsons.url;
														var weight = jsons.weight;
														//alert("id:"+mId+",name:"+title+",parentId:"+parentId+",path:"+path+",weight:"+weight);
														$.ajax({
															url:"../UpdateModule",
															data:{
																mId:mId,
																name:title,
																parentId:parentIds,
																path:path,
																weight:weight,
																token:token 
															},
															dataType:"json",
															type:"post",
															success:function(res){
																if(res.success){
																	layer.msg("修改成功");
																	window.location.reload();
																}else{
																	if(res.message==0){
																		layer.msg(res.remark,function(){
																			window.location="../login.html";
																		});
																	}else{
																		layer.msg("修改失败");
																		window.location.reload();
																	}
																}
															}
														})
													}
												}
											})
						    		}else if(type==='del'){				//删除
						    			//alert(obj.data.children);
						    			if(obj.data.children==undefined){		//判断是否存在子节点
						    				//alert("删除");
							    				$.ajax({
												url:"../DeleteModule",
												data:{
													mId:data.id,
													token:token
												},
												dataType:"json",
												type:"post",
												success:function(res){
													if(res.success){
														layer.msg("删除成功",function(){
															window.location.reload();
														});
													}else{
														if(res.message==0){
															layer.msg(res.remark,function(){
																window.location="../login.html";
															});
														}else{
															layer.msg("删除失败");
															window.location.reload();
														}
													}
												}
											})
						    			}else{
						    				layer.msg("该节点下存在子节点不可删除",function(){
						    					window.location.reload();
						    				});
						    			}
						    		}
						    }
						  });			
					}else{
				}
			}
		})
	})
})
	
//添加保存按钮
	function addmk(){
	var addname = $("#addname").val();
	var addparentId = $("#addparentId").val();
	var addpath = $("#addpath").val();
	var addweight = $("#addweight").val();
	$.ajax({
		url:"../CreateModule",
		data:{
			name:addname,
			parentId:addparentId,
			path:addpath,
			weight:addweight,
			token:token
		},
		dataType:"json",
		type:"post",
		success:function(res){
			if(res.success){
				layer.msg("添加成功",function(){
					layer.close(index);
					//indextree.reload();
					 window.location.reload();
				});
			}else{
				if(res.message==0){
					layer.msg(res.remark,function(){
						window.location="../login.html";
					});
				}else{
					layer.msg("添加失败",function(){
						layer.close(index);
						//indextree.reload();
						 window.location.reload();
					});
				}
			}
		}
	})
}
</script>
<!--添加窗体  下 -->
		<div  id="add" style="display:none">
		   <form id="adds" class="layui-form">
		   	<input type="text" id="addparentId" class="layui-input" readonly="readonly" style="display: none;">
		<div class="layui-inline">
			  <label class="layui-form-inline">模块名称：</label>
				<div class="layui-input-inline select">
				       <input type="text" id="addname" lay-verify="required" placeholder="" autocomplete="off" class="layui-input"><br/>
				  </div>
		  </div>
		  <div class="layui-inline">
			  <label class="layui-form-inline">模块对应的路径：</label>
				<div class="layui-input-inline select">
				       <input type="text" id="addpath" lay-verify="required" placeholder="" autocomplete="off" class="layui-input"><br/>
				  </div>
		  </div>
		  <div class="layui-inline">
			  <label class="layui-form-inline">权重整数：</label>
				<div class="layui-input-inline select">
				       <input type="text" id="addweight" lay-verify="required" placeholder="" autocomplete="off" class="layui-input"><br/>
				  </div>
		  </div>
		<div class="layui-inline count">
		    <button type="button" class="layui-btn layui-btn-warm"  onclick="addmk()">添加</button>
		    <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
		</div>
		   </form>
		
		</div>         
<!--添加窗体 上 -->
	</body>
</html>
